package com.dyn.videohub.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dyn.videohub.Common.R;
import com.dyn.videohub.Service.StarService;
import com.dyn.videohub.Service.UserService;
import com.dyn.videohub.domain.Star;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.joda.time.DateTime;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Controller
@RestController
@Api(tags = "收藏管理", value = "收藏接口")
@RequestMapping("/Star")
public class StarController {
    @Resource
    private StarService starService;
    @Resource
    private UserService userService;

    @ApiOperation(value = "添加收藏")
    @PutMapping("/addStar")
    public R<String> addStar(@RequestHeader String token, @RequestParam("videoId") Long videoId) {
        //获取当前时间
        DateTime currentTime = new DateTime();
        String datetimeString = currentTime.toString("yyyy-MM-dd HH:mm:ss");
        Long userId = userService.getCurrentUserId();
        Star star = new Star();
        star.setUserId(userId);
        star.setVideoId(videoId);
        star.setDate(datetimeString);
        starService.save(star);
        return R.success("收藏成功！");
    }

    @ApiOperation(value = "移除收藏")
    @PutMapping("/RmStar")
    public R<String> RmStar(@RequestParam("userId") Long userId, @RequestParam("videoId") Long videoId) {
        Star star = new Star();
        star.setUserId(userId);
        star.setVideoId(videoId);
        starService.removeById(star);
        return R.success("取消收藏成功！");
    }


    //获取用户所有收藏
    @ApiOperation(value = "获取用户所有收藏")
    @GetMapping("/getAll")
    public List<Star> getAll(@RequestParam("userId") Long userId) {
        LambdaQueryWrapper<Star> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Star::getUserId, userId);
        List<Star> starList = starService.list(lambdaQueryWrapper);
        return starList;
    }

    //判断用户是否收藏了此视频
    @ApiOperation(value = "用户收藏判断")
    @GetMapping("/judge")
    public String judge(@RequestParam("userId") Long userId, @RequestParam("videoId") Long videoId) {
        LambdaQueryWrapper<Star> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Star::getUserId, userId).eq(Star::getVideoId, videoId);
        Star star = starService.getOne(lambdaQueryWrapper);
        if (star == null) {
            return "false";
        }
        return "true";
    }
}
